//
//  baiduAPISendRequest.m
//  guess
//
//  Created by fank on 15/11/3.
//  Copyright © 2015年 fank. All rights reserved.
//

#import "BaiduAPISendRequest.h"


@implementation BaiduAPISendRequest
+(void)requestWithUrl:(NSString *)urlStr withPage:(NSNumber *)page withComplete:(void(^)(id data))complete  withfailure:(void(^)(id error))failure
{
    
    NSString *httpArg = [NSString stringWithFormat:@"page=%@",page];
    
    NSString *urlLong = [[NSString alloc]initWithFormat: @"%@?%@", urlStr, httpArg];
    
    NSURL *url = [NSURL URLWithString: urlLong];
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL: url cachePolicy: NSURLRequestUseProtocolCachePolicy timeoutInterval: 10];
    [request setHTTPMethod: @"GET"];
    [request addValue: KBaiDuApiKey forHTTPHeaderField: @"apikey"];
    NSMutableArray *arr  =[NSMutableArray array];
    NSMutableDictionary *dict =[[NSMutableDictionary alloc]init];
    [NSURLConnection sendAsynchronousRequest: request
                                       queue: [NSOperationQueue mainQueue]
                           completionHandler: ^(NSURLResponse *response, NSData *data, NSError *error){
                               if (error) {
                                   NSLog(@"Httperror: %@%ld", error.localizedDescription, (long)error.code);
                                   [dict setValue:@"no" forKey:@"success"];
                                   failure(@"网络连接故障，请稍后重试");
                                   
                               } else {
                                   NSInteger responseCode = [(NSHTTPURLResponse *)response statusCode];
                                   NSString *responseString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
                                   NSLog(@"HttpResponseCode:%ld", (long)responseCode);
                                   NSLog(@"HttpResponseBody %@",responseString);
                                   responseString =[responseString stringByReplacingOccurrencesOfString:@"</p>" withString:@""];
                                   responseString =[responseString stringByReplacingOccurrencesOfString:@"<p>" withString:@""];
                                   NSData *dataData =[responseString dataUsingEncoding:NSUTF8StringEncoding];
                                   
                                   complete(dataData);
                                
                               }
                           }];
}


+(void)requestWithUrl:(NSString *)url
                 page:(NSNumber *)page
              success:(void(^)(id data))success
                 fail:(void(^)(id error))fail
{
    NSURL *URL = [NSURL URLWithString: [NSString stringWithFormat:@"%@%@",url,page]];
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL: URL cachePolicy: NSURLRequestUseProtocolCachePolicy timeoutInterval: 5];
    [request setHTTPMethod: @"GET"];
    
    [NSURLConnection sendAsynchronousRequest:request
                                       queue:[NSOperationQueue mainQueue]
                           completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
                               if (connectionError) {
                                   fail(@"网络连接故障，请稍后重试");
                               }else {
                                   if (success) {
                                       success(data);
                                   }
                               }
                               
                                       }];

}
@end
